//
//  RenYuanGuanLiListVC.m
//  EnterpriseEdition
//
//  Created by ypj on 2021/10/9.
//

#import "RenYuanGuanLiListVC.h"
#import "RenYuanGuanLiCell.h"
#import "AddRenYuanGuanLiVC.h"
#import "RenYuanDetailInfoVC.h"
#import "RenYanGuanLiModel.h"
@interface RenYuanGuanLiListVC ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic,strong)  UITableView     *tableView;
@property (nonatomic, strong) NSMutableArray  *dataAry;
@property (nonatomic, assign) NSInteger        nowPage;
@property (nonatomic, assign) NSInteger        totalPage;

@end

@implementation RenYuanGuanLiListVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getRenYuanGuanLiList];
   
}

- (void)viewDidLoad {
     [super viewDidLoad];
     self.view.backgroundColor = White_Color;
    
    [self layoutCustomNav:self.view title:@"人员管理"];
    [self createRightButtonItem];
    
    [self dataAry];
     self.userInfo=[YLUserMgr fetchUser];
    [self createTableView];
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       [self getRenYuanGuanLiList];
    }];
    header.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = header;
    
    
    
}

-(void)createTableView{
    self.tableView = [[UITableView  alloc] initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, self.view.height - LL_TabbarSafeBottomMargin)];
    self.tableView.delegate = self;
    self.tableView.dataSource=self;
    self.tableView.backgroundColor = White_Color;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:[RenYuanGuanLiCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[RenYuanGuanLiCell reuseIdentifier]];
    [self.view addSubview:self.tableView];
}

#pragma mark - ------------------ DZNEmptyData ------------------
-(UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"No_order_icon"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"暂无数据";
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:15.0f],
                                 NSForegroundColorAttributeName:gray666_color
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    if (!self.tableView.mj_header.isRefreshing && !self.tableView.mj_footer.isRefreshing) {
        [self.tableView.mj_footer resetNoMoreData];
        self.nowPage = 1;
      //[self getOrderListWithOper];
    }
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -50;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataAry.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RenYuanGuanLiCell *cell = [tableView dequeueReusableCellWithIdentifier:[RenYuanGuanLiCell reuseIdentifier]];
    if (!cell) {
        cell=[[RenYuanGuanLiCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[RenYuanGuanLiCell reuseIdentifier]];
    }
    cell.selectionStyle= UITableViewCellSelectionStyleNone;
    RenYanGuanLiModel *model = self.dataAry[indexPath.row];
    cell.bieMingLb.text = model.realName;
    cell.jueSeLb.text = model.roleName;
    cell.nameLb.text = model.nickName;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [RenYuanGuanLiCell cellHeight];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RenYanGuanLiModel *model = self.dataAry[indexPath.row];

    RenYuanDetailInfoVC *VC = [[RenYuanDetailInfoVC alloc] init];
    VC.model = model;
    [self pushToNextController:VC];

}

#pragma mark ----------------<#note#>----------------
- (NSMutableArray *)dataAry {
    if (!_dataAry) {
        self.dataAry = [@[] mutableCopy];
    }
    return _dataAry;
}

-(void)createRightButtonItem{
    
    UIButton *eduitButton=[UIButton buttonWithType:UIButtonTypeCustom];
     eduitButton.frame=CGRectMake(ScreenWidth-45,  KStatuesBarHeight + 5, 30, 30);
    [eduitButton setImage:Image(@"AddPerson_icon") forState:UIControlStateNormal];
    [eduitButton addTarget:self action:@selector(EduitButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:eduitButton];
}

-(void)EduitButtonClick{
    AddRenYuanGuanLiVC *VC =[[AddRenYuanGuanLiVC alloc] init];
    [self pushToNextController:VC];
}

-(void)getRenYuanGuanLiList{
  WEAKSELF
    
    [MSNetworkManager postRevertUrl:POST_PersonList_URL params:@{} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            NSArray *cacheArray = resonseObj[@"records"];
            self.dataAry = [RenYanGuanLiModel mj_objectArrayWithKeyValuesArray:cacheArray];
           [weakSelf.tableView.mj_header endRefreshing];
           [self.tableView reloadData];
       }
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    
}

@end
